package com.video;

/*
 * @author:周洋帆
 * @description：根据关键词在优酷和土豆开放平台进行搜索，返回搜索结果。
 * @see：
 *     youkuAccess（keyWord）：根据关键词在优酷搜索，返回结果列表。
 *     tudouAccess（keyWord）：根据关键词在土豆搜索，返回结果列表。
 */

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class VideoSearch {
	public List<YoukuInfo> youkuAccess(String keyWord){
		List<YoukuInfo> list=new ArrayList<YoukuInfo>();
		CloseableHttpClient httpClient=HttpClients.createDefault();//用于网络连接
	    String result=null;
		
		try {
			//利用httpClient建立url方法，无需考虑转码问题。
			URI uri=new URIBuilder()
					.setScheme("http")
					.setHost("openapi.youku.com")
					.setPath("/v2/searches/video/by_keyword.json")
					.setParameter("client_id","3d5893ee126e0d96")
					.setParameter("keyword",keyWord)
					.setParameter("count", "100")
					.build();
			HttpGet httpGet=new HttpGet(uri);
			CloseableHttpResponse response=httpClient.execute(httpGet);
			HttpEntity entity=response.getEntity();
			result=EntityUtils.toString(entity);
//			System.out.println(result);
				
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(URISyntaxException e){
			e.printStackTrace();
		}finally{
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		JSONObject jo=JSON.parseObject(result);
		int total=jo.getIntValue("total");
//		System.out.println(total);
		JSONArray videos=jo.getJSONArray("videos");
		int length;
		if(total<=100)
			length=total;
		else{
			length=100;
		}
		for(int i=0;i<length;i++){
			YoukuInfo info=new YoukuInfo();
			JSONObject temp=videos.getJSONObject(i);
			String title=temp.getString("title");
			String link=temp.getString("link");
			String capture=temp.getString("thumbnail");
			String category=temp.getString("category");
			String tags=temp.getString("tags");
			int viewCount=temp.getIntValue("view_count");
			int favoriateCount=temp.getIntValue("favorite_count");
			if(category.equals("广告")){
				continue;
			}else{
				info.setCaptureUrl(capture);
				info.setCategory(category);
				info.setTags(tags);
				info.setVideoName(title);
				info.setVideoUrl(link);
				info.setViewCount(viewCount);
				info.setFavoriteCount(favoriateCount);
				list.add(info);
			}
	
		}//end for
		SearchAction.youkuNumber=list.size();
		return list;
	}
	
	public List<TudouInfo> tudouAccess(String keyWord){
		List<TudouInfo> list=new ArrayList<TudouInfo>();
		CloseableHttpClient httpClient=HttpClients.createDefault();
	    String result=null;
		
		try {
			URI uri=new URIBuilder()
					.setScheme("http")
					.setHost("api.tudou.com")
					.setPath("/v6/video/search")
					.setParameter("app_key","009f971f9ed00b79")
					.setParameter("kw",keyWord)
					.setParameter("pageSize", "100")
					.build();
//			System.out.println(uri);
			HttpGet httpGet=new HttpGet(uri);
			CloseableHttpResponse response=httpClient.execute(httpGet);
			HttpEntity entity=response.getEntity();
			result=EntityUtils.toString(entity);
//			System.out.println(result);
				
		}catch(URISyntaxException e){
			e.printStackTrace();
		}catch (IOException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				httpClient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		JSONObject jo=JSON.parseObject(result);
		JSONArray videos=jo.getJSONArray("results");
		int length;
		if(videos.size()>100){
			length=100;
		}
		else{
			length=videos.size();
		}
		for(int i=0;i<length;i++){
			TudouInfo info=new TudouInfo();
			JSONObject temp=videos.getJSONObject(i);
			String title=temp.getString("title");
			String link=temp.getString("playUrl");
			String capture=temp.getString("picUrl");
			String tags=temp.getString("tags");
			int playTimes=temp.getIntValue("playTimes");
			int favorCount=temp.getIntValue("favorCount");
			info.setCaptureUrl(capture);
			info.setTags(tags);
			info.setVideoName(title);
			info.setVideoUrl(link);
			info.setPlayTimes(playTimes);
			info.setFavorCount(favorCount);
			list.add(info);
		}
		SearchAction.tudouNumber=list.size();
		return list;
	}
	
	
}
